<template>
  <div class="admin-page">
    <div class="admin-menu">
      <div class="menu-item" :class="{ active: activeMenu === '/admin/user' }" @click="handlePage('/admin/user')">
        <i class="el-icon-user"></i>
        <span>用户管理</span>
      </div>
      <div v-if="userInfo && userInfo.id" class="menu-item logout" @click="handleLogout">
        <i class="el-icon-switch-button"></i>
        <span>退出登录</span>
      </div>
    </div>
    <div class="admin-content">
      <router-view />
    </div>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: 'Settings',
  data() {
    return {
      activeMenu: '/admin/user',
    }
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created() {
    this.activeMenu = this.$route.fullPath
  },
  methods: {
    handlePage(page) {
      this.activeMenu = page
      this.$router.push(page)
    },
    handleLogout() {
      this.$confirm('确认退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('user/logout')
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.admin-page {
  height: 100%;
  display: flex;
  background-color: #FAF9FA;

  .admin-header {
    margin-bottom: 24px;
    border-bottom: 1px solid #eee;
    padding:24px 48px;
    h1 {
      font-size: 28px;
      font-weight: 600;
      color: #1C1C28;
    }
  }
  .admin-menu {
    flex-shrink: 0;
    width: 230px;
    padding: 32px 24px;
    display: flex;
    flex-direction: column;
    gap: 8px;

    .menu-item {
      height: 40px;
      display: flex;
      align-items: center;
      padding: 0 16px;
      border-radius: 12px;
      cursor: pointer;
      transition: all 0.3s ease;

      i {
        font-size: 18px;
        margin-right: 12px;
      }

      span {
        font-size: 14px;
        color: #302e34;
      }

      &:hover {
        background: rgba(126, 76, 224, 0.04);
        color: #7E4CE0;
      }

      &.active {
        background: #7E4CE0;
        color: #fff;

        span {
          color: #fff;
        }

        i {
          color: #fff;
        }
      }

      &.logout {
        margin-top: auto;
        color: #F56C6C;

        &:hover {
          background: rgba(245, 108, 108, 0.1);
        }
      }
    }
  }

  .admin-content {
    flex-grow: 1;
    background-color: #fff;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }
}
</style>
